package server;

import java.io.IOException;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import server.network.ConnectionListener;
import server.network.ServerListener;
import shared.network.messages.TextMessage;

import com.jme3.app.SimpleApplication;
import com.jme3.network.Network;
import com.jme3.network.Server;
import com.jme3.network.serializing.Serializer;
import com.jme3.system.JmeContext;

public class ServerMain extends  SimpleApplication {

	public final int PORT = 6143;
	
	private ConnectionListener conlistener = new ConnectionListener();
	
	private Server server;
	
	private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(8);
	
	@Override
	public void simpleInitApp() {
		try {
			//Register Classes
			Serializer.registerClass(TextMessage.class);
			
			//Create Server
			server = Network.createServer(PORT);
			server.start();
			
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		
		server.addConnectionListener(conlistener);
		server.addMessageListener(new ServerListener());

		System.out.println("is running: " + server.isRunning() + ", " + server.getGameName() + ", " + server.getVersion());
		
		//Just testing
		TextMessage t = new TextMessage("Client disconnected", "DEBUG");
		System.out.println(t.toString());
		
	}

	  @Override
	  public void destroy() {
	   
	      server.close();
	      super.destroy();
	  }
	
	public static void main(String[] args)
	{
		ServerMain app = new ServerMain();
		app.start(JmeContext.Type.Headless);
	}
}
